package com.kinyx.framework.utils;

import java.io.Serializable;
import java.util.function.Function;

import javax.util.XArrayList;

import org.junit.Assert;
import org.junit.Test;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

public class KListUtils_Tester_convert {

	@Getter
	@Setter
	@AllArgsConstructor
	@ToString
	@SuppressWarnings("serial")
	public static class Bean implements Serializable { private final String value; }

	@Test
	public void test() {
		Assert.assertNull(KListUtils.convert(null, null));

		final Function<String, String> converterString = source -> { return source == null ? null : "from: " + source; };
		Assert.assertEquals("[]", KListUtils.toString(KListUtils.convert(new XArrayList<String>(), converterString)));

		Assert.assertEquals("[null]", KListUtils.toString(KListUtils.convert(new XArrayList<String>().xAdd(null), converterString)));
		Assert.assertEquals("[from: foo]", KListUtils.toString(KListUtils.convert(new XArrayList<String>().xAdd("foo"), converterString)));

		final Function<Bean, String> converterBean = source -> { return source == null ? null : "from: " + source.value; };
		Assert.assertEquals("[null]", KListUtils.toString(KListUtils.convert(new XArrayList<Bean>().xAdd(null), converterBean)));
		Assert.assertEquals("[from: foo]", KListUtils.toString(KListUtils.convert(new XArrayList<Bean>().xAdd(new Bean("foo")), converterBean)));
		Assert.assertEquals("[null, from: foo]", KListUtils.toString(KListUtils.convert(new XArrayList<Bean>().xAdd(null).xAdd(new Bean("foo")), converterBean)));
	}

}
